<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>上行竞赛</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">

    <!--    <link rel="stylesheet" href="./lib/layui/css/layui.css">-->
    <!--    <link rel="stylesheet" href="./css/index.css">-->
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script>
        // 是否开启刷新记忆tab功能
        // var is_remember = false;
    </script>

    <style>
        @font-face {
            font-family: Gotham;//自定义字体名称
        src: url('./fonts/Gotham-LightItalic.otf')
        }

        html {
            margin: 0;
            padding: 0;
            width: 100%;
            /*height: 100%;*/
            background: #f5f5f5;
        }

        body {
            margin: 0;
            padding: 0;
            width: 100%;
            /*height: 100%;*/
            font-family: Gotham;
        }

        .layui-row .comp-name>a:hover {
            color: #00c1de;
        }

        .banner {
            margin-top: 20px;
            height: 340px;
            background: rgba(24, 47, 99, .3)
        }

        .contents {
            padding-top: 40px;
            width: 100%;
            height: 100%;
            background: #f5f5f5;
        }

        .comp-item {
            margin-left: 180px;
            margin-right: 180px;
            margin-bottom: 20px;
            padding: 20px 0 0 30px;
            background: #ffffff
        }

        .comp-name {
            font-size: 22px;
            line-height: 38px
        }

        .active-tag {
            background-color: #FFF7E6;
            line-height: 18px;
            padding: 4px 8px;
            margin-left: 10px;
            font-size: 14px;
            color: #fa8c16;
            border-radius: 4px;
            margin-top: 7px;
            position: absolute;
        }

        .inactive-tag {
            background-color: #B2B2B2;
            line-height: 18px;
            padding: 4px 8px;
            margin-left: 10px;
            font-size: 14px;
            color: #FFFFFF;
            border-radius: 4px;
            margin-top: 7px;
            position: absolute;
        }

        .info-row {
            padding: 15px 0 0 0
        }

        .comp-col {
            height: 130px
        }

        .owner {
            padding-top: 20px;
            font-size: 12px;
            color: #999999
        }

        .comp-desc {
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            margin-bottom: 15px;
            padding-right: 100px;
            font-size: 12px;
            color: #999999;
            /*word-break: break-all;*/
        }

        .reward-title {
            color: #515151;
            font-size: 12px;
            padding: 0 0 20px 0;
        }

        .reward-content {
            color: #515151;
            font-size: 22px;
            /*padding: 0px 0 12px 0;*/
        }

        .team-title {
            color: #515151;
            font-size: 12px;
            padding: 0 0 20px 0;
        }

        .team-content {
            color: #515151;
            font-size: 22px;
            /*padding: 0px 0 12px 0;*/
        }

        .season-title {
            color: #515151;
            font-size: 12px;
            padding: 0 0 20px 0;
        }

        .season-content {
            color: #515151;
            font-size: 22px;
            /*padding: 0px 0 12px 0;*/
        }

        .page {
            position: relative;
            margin-right: 194px;
            float: right;
            background-color: #f5f5f5;
            margin-top: 8px;
            display: none;
            /*visibility: hidden;*/
        }

        .page a {
            display: inline-block;
            background: #fff;
            color: #888;
            padding: 0 6px 0 6px;
            margin: 3px;
            border-radius: 4px;
            min-width: 15px;
            border: 1px solid #E2E2E2;
        }

        .layui-laypage a:hover {
            color: #00c1de;
        }

        .page span {
            display: inline-block;
            padding: 0 6px 0 6px;
            min-width: 15px;
            border-radius: 4px;
            margin: 3px;
            border: 1px solid #E2E2E2;
        }

        .layui-laypage > a:first-child, .layui-laypage > a:first-child em {
            border-radius: 4px;
        }

        .layui-laypage > a:last-child, .layui-laypage > a:last-child em {
            border-radius: 4px;
        }
        .layui-nav .layui-this:after, .layui-nav-bar, .layui-nav-tree .layui-nav-itemed:after {
            background-color: rgba(0,0,0,0);
        }

    </style>
    <!--可以在收藏夹中显示出图标-->
    <link rel="Bookmark" type="image/png" href="./img/logo.png"/>
    <!--可以在地址栏中显示出图标-->
    <link rel="icon" type="image/png" href="./img/logo.png"/>
    <link rel="shortcut icon" type="image/png" href="./img/logo.png"/>
</head>
<body>
<div id="body" style="height: 100%;width: 100%;background-color: #f5f5f5">
    <!-- 顶部开始 -->
    <div class="container">
        <div class="logo">
            <a>上行竞赛平台</a>
        </div>

        <ul class="layui-nav right">
            <li class="layui-nav-item" id="login" style="display: none">
                <a href="javascript:;">登录</a>
            </li>
            <li class="layui-nav-item" id="register" style="display: none">
                <a href="javascript:;">注册</a>
            </li>
            <li class="layui-nav-item" id="login-user" style="display: none">
                <a href="javascript:;" id="user-name"></a>
            </li>
        </ul>
    </div>
    <!-- 顶部结束 -->
    <div class="container banner">
        <img src="./img/banner-1440-340.png" style="width: 100%;height: 100%">
    </div>
    <div class="layui-container contents" id="comp-list">
    </div>
    <div id="page" class="page"></div>
</div>
</body>
<script src="./lib/jquery.min.js"></script>
<script src="./js/const.js"></script>
<script src="./js/util.js"></script>
<script>
    function checkLoginState() {
        util.sendGET('/check_login_state',function (response) {
            if (response.status){
                userInfo = util.getLocalStorage("user");
                console.log(userInfo);
                if(userInfo != null && typeof(userInfo) != 'undefined'){
                    $("#login").hide();
                    $("#register").hide();
                    $("#user-name").html(userInfo.userName);
                    $("#login-user").show();
                }
            }else{
                util.removeLocalStorage("user");
                $("#login").show();
                $("#register").show();
                $("#user-name").html("");
                $("#login-user").hide();
            }
        },function (response) {

        });
    }
    checkLoginState();
    util.sendGET("/competition_list?page=1&limit=10", function (response) {
        console.log(response);
        var total = response.total;
        var data = response.data;
        render_competition_list(data);
        if (total > 10) {
            $("#page").show();
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    groups: 3,
                    count: total, //数据总数，从服务端得到
                    theme: '#00c1de',
                    layout: ['prev', 'page', 'next'],
                    prev: '<',
                    next: '>',
                    jump: function (obj, first) {
                        //obj包含了当前分页的所有参数
                        //首次不执行
                        if (!first) {
                            util.sendGET("/competition_list?page="+obj.curr+"&limit="+obj.limit, function (response) {
                                    render_competition_list(response.data);
                                    // $("#body").scrollTop=0;
                                    $("html,body").animate({scrollTop:0},0);
                                },
                                function (response) {
                                    layer.msg("数据获取失败", {icon: 2});
                                });
                        }
                    }
                });
            });
        }

    }, function (response) {
        layer.msg("数据获取失败", {icon: 2});
    });

    function render_competition_list(data) {
        var is_first=true;
        for (var i=0; i<data.length;i++ ) {
            var item = data[i];
            // console.log(item);
            var comp_name = item.competitionName;
            var competition_id = item.competitionId;
            var state_style = '';
            var state = '';
            var season = '';
            var season_time = '';
            var joins = item.joins;
            switch (item.state) {
                case 0:
                    state_style = "inactive-tag";
                    state = "未开始";
                    season = "未开始";
                    season_time = item.startTime.split(' ')[0];
                    break;
                case 1:
                    state_style = "active-tag";
                    state = "进行中";
                    season = "A榜";
                    season_time = item.endTimeA.split(' ')[0];
                    break;
                case 2:
                    state_style = "active-tag";
                    state = "进行中";
                    season = "B榜";
                    season_time = item.endTimeB.split(' ')[0];
                    break;
                case 3:
                    state_style = "inactive-tag";
                    state = "已结束";
                    season = "B榜";
                    season_time = item.endTimeB.split(' ')[0];
                    break;
            }
            var comp_desc = item.competitionDesc;
            var owner = item.owner;
            var comp_item = '<div class="layui-row comp-item">' +
                            '<div class="layui-row comp-name">' +
                            '<a href="./html/competition.html?competitionId='+competition_id+'" target="_blank" style="text-align: center">' + comp_name + ' <span class=' + state_style + '>' + state + '</span></a>'+
                            '</div>' +
                            '<div class="layui-row info-row">' +
                            '<div class="layui-col-lg7 comp-col">' +
                            '<div class="layui-row comp-desc">' +
                            comp_desc +
                            '</div>' +
                            '<div class="layui-row owner">' +
                            '举办方：' + owner +
                            '</div>' +

                            '</div>' +
                            '<div class="layui-col-lg2">' +
                            '<div class="layui-row reward-title">' +
                            '开始日期' +
                            '</div>' +
                            '<div class="layui-row reward-content">' +
                            item.startTime.split(' ')[0]+
                            '</div>' +
                            '</div>' +
                            '<div class="layui-col-lg1">' +
                            '<div class="layui-row team-title">' +
                            '参赛团队' +
                            '</div>' +
                            '<div class="layui-row team-content">' +
                            joins +
                            '</div>' +
                            '</div>' +
                            '<div class="layui-col-lg2">' +
                            '<div class="layui-row season-title">' +
                            '赛季-' + season +
                            '</div>' +
                            '<div class="layui-row season-content">' +
                            season_time +
                            '</div>' +
                            '</div>' +
                            '</div>' +
                            '</div>';

            if (is_first){
                console.log(item);
                console.log(comp_item);
                $("#comp-list").html(comp_item);
            } else {
                $("#comp-list").append(comp_item);
            }
            is_first = false;
        }
    }
</script>
<script>
    $("#login").click(function () {
        open('用户登录','./html/login.html',700,500);
    });
    $("#register").click(function () {
        open('用户注册','./html/register.html',700,500);
    });
    $("#user-name").click(function () {
        open('个人信息','./html/user_update.html',700,500);
    });
    function open(title,url,width,height) {
        var index = layer.open({
            type: 2,
            area: [width+'px', height +'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: title,
            content: url,
            end:function () {
                checkLoginState();
            }
        });
    }

</script>
</html>